package com.lframework.xingyun.basedata.impl.shop;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lframework.starter.web.core.annotations.oplog.OpLog;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.OpLogUtil;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import java.io.Serializable;
import com.lframework.starter.common.utils.Assert;
import com.lframework.starter.web.gen.impl.GenCustomListToolbarServiceImpl;
import com.lframework.starter.web.inner.components.oplog.OtherOpLogType;
import com.lframework.xingyun.basedata.entity.Shop;
import com.lframework.xingyun.basedata.entity.ShopProduct;
import com.lframework.xingyun.basedata.mappers.ShopProductMapper;
import com.lframework.xingyun.basedata.service.shop.ShopProductService;
import com.lframework.xingyun.basedata.service.shop.ShopService;
import com.lframework.xingyun.basedata.vo.shop.QueryShopProductVo;
import com.lframework.xingyun.basedata.vo.shop.SaveShopProductVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ShopProductServiceImpl extends BaseMpServiceImpl<ShopProductMapper, ShopProduct> implements ShopProductService {

    @Override
    public PageResult<ShopProduct> queryPage(Integer pageIndex, Integer pageSize, QueryShopProductVo vo) {

        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        IPage<ShopProduct> page = this.baseMapper.queryPage(vo);

        return PageResultUtil.convert(page);
    }

    @Override
    public List<ShopProduct> queryList(QueryShopProductVo vo) {

        return getBaseMapper().queryList(vo);
    }

    @Cacheable(value = ShopProduct.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
    @Override
    public ShopProduct findById(String id) {

        return getBaseMapper().selectById(id);
    }


    @OpLog(type = OtherOpLogType.class, name = "门店授权商品，shopId：{}, productAll：{}, productIds：{}", params = {"#shopId", "#productAll", "#productIds"})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SaveShopProductVo vo) {
        getThis(ShopProductService.class).remove(Wrappers.lambdaQuery(ShopProduct.class).eq(ShopProduct::getShopId, vo.getShopId()));

        getThis(ShopService.class).update(Wrappers.lambdaUpdate(Shop.class)
                .set(Shop::getProductAll, vo.getProductAll())
                .eq(Shop::getId, vo.getShopId()));

        getThis(ShopService.class).cleanCacheByKey(vo.getShopId());

        List<ShopProduct> shopProducts = StrUtil.split(vo.getProductIds(), StrUtil.COMMA).stream().map(productId -> {
            ShopProduct shopProduct = new ShopProduct();
            shopProduct.setShopId(vo.getShopId());
            shopProduct.setProductId(productId);
            return shopProduct;
        }).collect(Collectors.toList());

        getThis(ShopProductService.class).saveBatch(shopProducts);

        OpLogUtil.setVariable("shopId", vo.getShopId());
        OpLogUtil.setVariable("productAll", vo.getProductAll());
        OpLogUtil.setVariable("productIds", vo.getProductIds());
        OpLogUtil.setExtra(vo);
    }

    @CacheEvict(value = ShopProduct.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {

    }

}

